var map = new OpenLayers.Map();


map.addControl(new OpenLayers.Control.LayerSwitcher());

//set background layers
var gphy = new OpenLayers.Layer.Google("Google Physical", {
    type: google.maps.MapTypeId.TERRAIN
});
var gmap = new OpenLayers.Layer.Google("Google Streets", // the default
{
    numZoomLevels: 20
});
var ghyb = new OpenLayers.Layer.Google("Google Hybrid", {
    type: google.maps.MapTypeId.HYBRID,
    numZoomLevels: 20
});
var gsat = new OpenLayers.Layer.Google("Google Satellite", {
    type: google.maps.MapTypeId.SATELLITE,
    numZoomLevels: 22
});

var osm = new OpenLayers.Layer.OSM();

map.addLayers([osm, gphy, gmap, ghyb, gsat]);

//set vector layer
var vector = new OpenLayers.Layer.Vector("vector");

map.addLayers([vector]);


//set controls
var ctrl, toolbarItems = [], action, actions = {};

//navigation button
action = new GeoExt.Action({
    text: "Pan",
    control: new OpenLayers.Control.Navigation(),
    map: map,
    // button options
    toggleGroup: "draw",
    allowDepress: false,
    pressed: true,
    tooltip: "navigate",
    // check item options
    group: "draw",
    checked: true
});

actions["nav"] = action;

toolbarItems.push(action);


//draw button
action = new GeoExt.Action({
    text: "Draw Impact Area",
    control: new OpenLayers.Control.DrawFeature(vector, OpenLayers.Handler.Polygon),
    map: map,
    // button options
    toggleGroup: "draw",
    allowDepress: false,
    tooltip: "draw polygon",
    // check item options
    group: "draw"
});

actions["drawpolygon"] = action;
toolbarItems.push(action);

//edit button
action = new GeoExt.Action({
    text: "Edit",
    control: new OpenLayers.Control.ModifyFeature(vector),
    map: map,
    // button options
    toggleGroup: "draw",
    allowDepress: false,
    tooltip: "Edit",
    // check item options
    group: "draw"
});

actions["edit"] = action;

toolbarItems.push(action);


// select button
action = new GeoExt.Action({
    text: "Select",
    control: new OpenLayers.Control.SelectFeature(vector, {
        type: OpenLayers.Control.TYPE_TOGGLE,
        hover: false
    }),
    map: map,
    // button options
    //enableToggle: true,
    group: 'draw',
    toggleGroup: "draw",
    tooltip: "select feature"
});

actions["select"] = action;

toolbarItems.push(action);

//set marker button
action = new GeoExt.Action({
    text: 'Set Marker',
    handler: function(){
        var i, fe;
        var fes = vector.features;
        var impacts = [];
        for (i = 0; i < fes.length; i++) {
        
            fe = fes[i];
            
            if (fe.geometry.CLASS_NAME == "OpenLayers.Geometry.Polygon") {
                //it is an impact
                var pt = new OpenLayers.Feature.Vector(fe.geometry.getCentroid());
                pt.attributes['parent'] = fe.id;
                
                impacts.push(pt)
                //pt.attributes['parent'] = fe.id;
            }
        }
        vector.addFeatures(impacts);
        
        
    }
    
});

toolbarItems.push(action);

action = new GeoExt.Action({
    text: 'Delete',
    handler: function(){
        var i, fe;
        var fes = vector.selectedFeatures;
        var impacts = [];
        for (i = 0; i < fes.length; i++) {
            fe = fes[i];
            fe.destroy();
        }
    }

});
actions['delete'] = action;
toolbarItems.push(action);
toolbarItems.push("-");

// Navigation history - two "button" controls
ctrl = new OpenLayers.Control.NavigationHistory();
map.addControl(ctrl);

action = new GeoExt.Action({
    text: "previous",
    control: ctrl.previous,
    disabled: true,
    tooltip: "previous in history"
});
actions["previous"] = action;
toolbarItems.push(action);

action = new GeoExt.Action({
    text: "next",
    control: ctrl.next,
    disabled: true,
    tooltip: "next in history"
});
actions["next"] = action;
toolbarItems.push(action);

toolbarItems.push("->");

action = new GeoExt.Action({
    text: 'Submit Form',
    handler: function(){
        var i, fe;
        var fes = vector.features;
        var impacts = [];
        var results = '';
        for (i = 0; i < fes.length; i++) {
            fe = fes[i];
            if (fe.geometry.CLASS_NAME == "OpenLayers.Geometry.Polygon") {
                //it is an impact
                results = results + '\n' + 'polygon: ' + fe.id + '\n' + fe.geometry.toString();
                
            }
            else 
                if (fe.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
                    //it is an marker
                    results = results + '\n' + 'The center of : ' + fe.attributes.parent + '\n' + fe.geometry.toString();
                }
            
        }
        
        alert(results)
    }
});

toolbarItems.push(action);



var mapPanel = new GeoExt.MapPanel({
    height: 400,
    width: 600,
    map: map,
    center: new OpenLayers.LonLat(-83.019919, 40.001405).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()),
    zoom: 15,
    tbar: toolbarItems
});
